package com.hsq.androidproject.ui.find_fragment.taolun;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.adapter.MyListViewFindTopicsAdapter;
import com.hsq.androidproject.entity.find.FindMineTL;
import com.hsq.androidproject.utils.OkHttpClientManager;
import com.hsq.androidproject.utils.glide.GlideCircleTransform;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.okhttp.Request;

import java.util.List;

public class ShowMineActivity extends FragmentActivity {
    private TextView tv_name;       /////讨论区  或者 热门人的title
    private ImageView iv_img;   /////头像
    private TextView tv_title;  ////标题
    private TextView tv_topicCount;    ////话题数

    private PullToRefreshListView lv_home;
    //当前页数
    private int curPage = 1;
    private View footView;
    private MyListViewFindTopicsAdapter adapter;
    private List<FindMineTL.TopicsBean> topicsBean;
    private List<FindMineTL.TopicsBean> topicsBean1;
    private String path;
    private int bid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_mine);
        Intent intent = getIntent();        ///////MyListViewFindMineAdapter我的讨论区 传输过来的数据
        bid = intent.getIntExtra("bid", 0);
        initView();
        tv_name.setText("讨论区        ");

        addData1();
        lv_home.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() { //下拉刷新
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                addData1();
            }
        });
        lv_home.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                requestData(curPage + 1);
            }
        });

    }

    private void addData1() {
        path = "http://api.hanju.koudaibaobao.com/bbs/api/forum/topicsV2?bid=" + bid + "&page=" + curPage;
        OkHttpClientManager.getAsyn(path, new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                FindMineTL findMineTL = new Gson().fromJson(response, FindMineTL.class);
                FindMineTL.BoardBean board = findMineTL.getBoard();

                tv_title.setText(board.getTitle());
                tv_topicCount.setText(board.getTopicCount() + "");
                Glide.with(ShowMineActivity.this)  //头像
                        .load(board.getThumb())
                        .transform(new GlideCircleTransform(ShowMineActivity.this))//圆形
                        .into(iv_img);

                topicsBean = findMineTL.getTopics();

                adapter = new MyListViewFindTopicsAdapter(topicsBean,board.getTitle(), ShowMineActivity.this);
                lv_home.setAdapter(adapter);
                lv_home.onRefreshComplete();
            }
        });
    }

    private void initView() {
        tv_name = (TextView) findViewById(R.id.tv_name);
        iv_img = (ImageView) findViewById(R.id.iv_img);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_topicCount = (TextView) findViewById(R.id.tv_topicCount);
        lv_home = (PullToRefreshListView) findViewById(R.id.lv_home);
    }

    private void requestData(final int curPage) {
        //初始化底部加载更多布局
        footView = View.inflate(ShowMineActivity.this, R.layout.footview_loading, null);
        path = "http://api.hanju.koudaibaobao.com/bbs/api/forum/topicsV2?bid=" + bid + "&page=" + curPage;
        if (curPage == 1) {
            lv_home.onRefreshComplete();
        } else {
            OkHttpClientManager.getAsyn(path, new OkHttpClientManager.ResultCallback<String>() {
                @Override
                public void onError(Request request, Exception e) {

                }

                @Override
                public void onResponse(String response) {
                    addFootView(lv_home, footView);
                    FindMineTL findMineTL = new Gson().fromJson(response, FindMineTL.class);
                    topicsBean1 = findMineTL.getTopics();    //  得到集合数据
                    for (int i = 0; i < topicsBean1.size(); i++) {
                        topicsBean.add(topicsBean1.get(i));
                    }
                    //page为1 的时候 清理集合 ,否则不清理集合 调用下面的方法
                    adapter.notifyDataSetChanged();
                    lv_home.onRefreshComplete();  //完成
                }
            });
        }

    }

    private void addFootView(PullToRefreshListView lv_home, View footView) {
        ListView listView = lv_home.getRefreshableView();

        if (listView.getFooterViewsCount() == 1) {
            listView.addFooterView(footView);
        }
    }

    public void click_back(View view) {
        finish();
    }
}
